/**
    * 结算  
    * @/views/budget/settlement/index.vue
    * 2020-12-29
    * silas
 */
<template>
  <div class="bigOrder_container">
    <header>
      <p>网间结算话单统计</p>
      <!-- <el-button size="mini" @click="handle_back">返回</el-button> -->
    </header>
    <main>
      <el-form :inline="true" :model="form" class="main_searchBar" ref="form">
        <el-form-item label="区域:">
          <dx-cascader :area_id.sync="form.areaId" ref="cascader" />
        </el-form-item>
        <el-form-item label="月份:">
          <el-date-picker size="mini" v-model="form.startDate" value-format="yyyyMM" type="month" placeholder="选择月份" :picker-options="pickerBeginDateBefore"></el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="结束日期:">
          <el-date-picker size="mini" v-model="form.endDate" value-format="yyyyMM" type="month" placeholder="选择结束日期" :picker-options="pickerBeginDateAfter"></el-date-picker>
        </el-form-item>-->
        <el-form-item>
          <el-button size="mini" id="search_btn" @click="handle_search">查询</el-button>
          <el-button size="mini" id="export_btn" @click="handle_export">导出</el-button>
        </el-form-item>
      </el-form>
      <div class="table">
        <el-tabs v-model="tabActive" type="card" style="margin-top: 10px;">
          <el-tab-pane label="短信结算" name="list1"></el-tab-pane>
          <el-tab-pane label="语音结算" name="list2"></el-tab-pane>
        </el-tabs>
        <div v-show="tabActive=='list1'">
          <el-table
            :max-height="height+'px'"
            id="table1"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData1"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column
              :min-width="item.width"
              align="center"
              v-for="(item, index) in titleList1"
              :key="index"
              :label="item.name"
              :prop="item.prop"
            
              show-overflow-tooltip
            ></el-table-column>
          </el-table>
        </div>
        <div v-show="tabActive=='list2'">
          <el-table
            :max-height="height+'px'"
            id="table2"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData2"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column
              :min-width="item.width"
              align="center"
              v-for="(item, index) in titleList2"
              :key="index"
              :label="item.name"
              :prop="item.prop"
              show-overflow-tooltip
            ></el-table-column>
          </el-table>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
// 引入导出Excel表格依赖
import FileSaver from 'file-saver';
import XLSX from 'xlsx';
import { $budgetAPI } from '@/api';
import { setSessionStorage, getSessionStorage, removeSessionStorage } from '@/common/js/storage';
export default {
    data() {
        return {
            form: {
                areaId: '',
                startDate: ''
                // endDate: ''
            },
            // 开始时间disabled
            pickerBeginDateBefore: {
                disabledDate: time => {
                    return time.getTime() > Date.now();
                }
            },
            // 结束时间disabled
            // pickerBeginDateAfter: {
            //     disabledDate: time => {
            //         return time.getTime() > Date.now();
            //     }
            // },
            currentDate: '',
            height: '',
            tabActive: 'list1',
            titleList1: [
                {
                    name: '区域名称',
                    prop: 'areaName',
                    width: 120
                },
                {
                    name: '区域层级',
                    prop: 'areaLvl',
                    width: 120
                },
                {
                    name: '月份',
                    prop: 'cycleMonth',
                    width: 160
                },
                {
                    name: '报表序列',
                    prop: 'torder',
                    width: 120
                },
                {
                    name: '结算科目编号',
                    prop: 'tnode',
                    width: 120
                },
                {
                    name: '结算科目',
                    prop: 'tname',
                    width: 140
                },
                {
                    name: '话单数',
                    prop: 'count',
                    width: 120
                },
                {
                    name: '结算费用',
                    prop: 'settfee',
                    width: 160
                }
            ],
            titleList2: [
                {
                    name: '区域名称',
                    prop: 'areaName',
                    width: 120
                },
                {
                    name: '区域层级',
                    prop: 'areaLvl',
                    width: 120
                },
                {
                    name: '月份',
                    prop: 'cycleMonth',
                    width: 160
                },
                {
                    name: '报表序列',
                    prop: 'torder',
                    width: 120
                },
                {
                    name: '结算科目编号',
                    prop: 'tnode',
                    width: 120
                },
                {
                    name: '结算科目',
                    prop: 'tname',
                    width: 140
                },
                {
                    name: '跳次',
                    prop: 'count',
                    width: 120
                },
                {
                    name: '结算时长',
                    prop: 'settmin',
                    width: 120
                },
                {
                    name: '结算费用',
                    prop: 'settfee',
                    width: 160
                }
            ],
            tableData1: [],
            tableData2: [],
            tableLoading: false,
            accountInfo: null,
            tableFixed: true,
        };
    },
    methods: {
        getHeight() {
            this.height = window.innerHeight - this.$refs.form.$el.offsetHeight - 83 - 54 - 51 - 40 - 10;
        },
        // changeTime() {
        //     let self = this;
        //     self.form.endDate = '';
        //     self.pickerBeginDateAfter = {
        //         disabledDate(time) {
        //             let timeyear = time.getFullYear();
        //             let timemonth = time.getMonth() + 1;
        //             if (timemonth >= 1 && timemonth <= 9) {
        //                 timemonth = '0' + timemonth;
        //             }
        //             let currentTime = timeyear.toString() + timemonth.toString();
        //             if (currentTime < self.form.startDate || currentTime > self.currentDate) {
        //                 return true;
        //             }
        //         }
        //     };
        // },
        getCurrentTimeStr() {
            var currentDate = new Date();
            var currentTimeStrBefore = null;
            var currentTimeStr = null;
            var year = currentDate.getFullYear().toString();
            var month = (currentDate.getMonth() + 1).toString();
            currentTimeStrBefore = year + month;
            currentTimeStr = year + month;
            this.currentDate = currentTimeStr;
            this.form.startDate = currentTimeStrBefore;
            // this.form.endDate = currentTimeStr;
        },
        handle_search() {
            this.tableLoading = true;
            switch (this.tabActive) {
                case 'list1':
                    this.getBill();
                    break;
                case 'list2':
                    this.getOrder();
                    break;
            }
        },

        // 导出
        handle_export() {
            var that = this;
                that.loading = this.$loading({
                    lock: true,
                    text: "导出中",
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.7)",
                });

            
                let workbook = XLSX.utils.book_new();
                let ws1 = XLSX.utils.table_to_sheet(document.querySelector('#table1'), { raw: true });
                XLSX.utils.book_append_sheet(workbook, ws1, '短信结算');
                let ws2 = XLSX.utils.table_to_sheet(document.querySelector('#table2'),  { raw: true });
                XLSX.utils.book_append_sheet(workbook, ws2, '语音结算');
                let wbout = XLSX.write(workbook, {
                    bookType: 'xlsx',
                    bookSST: true,
                    type: 'array'
                });
                try {
                    FileSaver.saveAs(
                        new Blob([wbout], {
                            type: 'application/octet-stream'
                        }),
                        '网间结算话单统计.xlsx'
                    );
                    that.loading.close();
                } catch (e) {
                    if (typeof console !== 'undefined') console.log(e, wbOut);
                }
                that.tableFixed = true;
                return wbout;
        },
        getBill() {
            $budgetAPI.getSMSsettlement(
                {
                    'cycleMonth':  this.form.startDate,
                    'areaId': this.form.areaId
                },
                res => {
                    if (res.code == 0) {
                        if (res && res.content) {
                            this.tableData1 = res.content;
                        }
                    }
                    this.tableLoading = false;
                },
                err => {
                    this.tableLoading = false;
                }
            );
        },
        getOrder() {
            $budgetAPI.getVoiceSettlement(
                {
                    'cycleMonth':  this.form.startDate,
                    'areaId': this.form.areaId
                },
                res => {
                    if (res.code == 0) {
                        if (res && res.content ) {
                            this.tableData2 = res.content;
                        }
                    }
                    this.tableLoading = false;
                },
                err => {
                    this.tableLoading = false;
                }
            );
        },
        getInitData() {
            this.tableLoading = true;
            this.getBill();
            this.getOrder();
        }
    },
    mounted() {
        this.getHeight();
        // 获取账户信息
        this.accountInfo = JSON.parse(getSessionStorage('accountInfo'));
        this.form.areaId = this.accountInfo.areaId;
        this.getCurrentTimeStr();

        $budgetAPI.incomeKeepRegionalMonth({
            maximum: 11,
          },
          res => {
            if (res.code == 0) {
                this.form.startDate = res.content;
                this.getInitData();
            }
          },
          err => {
            console.log(err);
          }
        );
       
    },
    created() {
        window.addEventListener('resize', this.getHeight);
    },
    destroyed() {
        window.removeEventListener('resize', this.getHeight);
    }
};
</script>

<style lang="scss" scoped>
// @import './index.scss';
.bigOrder_container {
    .hover {
        cursor: pointer;
        color: #3456dc;
    }
    .num {
        &:hover {
            color: #3456dc;
        }
    }
    &>header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #eaeaea;
        height: 40px;
        margin: 0 -20px;
        padding: 0 20px;
        &>p {
            font-weight: bold;
            font-size: 15px;
        }
    }
    &>main {
        padding: 0 10px 10px;
        /deep/ .el-table {
            .el-table__body-wrapper {
                th {
                    &>.cell {
                        white-space: nowrap;
                    }
                }
            }
        }
        .main_searchBar {
            border-bottom: 1px solid #d6d9e5;
            /deep/ .el-form-item {
                margin: 6px 10px 6px 0;
            }
            button {
                color: #fff;
                border: none;
                outline: none;
                &:hover {
                    opacity: 0.8;
                }
            }
            #search_btn {
                background-color: #2a90de;
            }
            #export_btn {
                background-color: #2cd531;
            }
        }
        .main_navBar {
            padding: 10px 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .area {
                text-align: left;
                display: flex;
                .it {
                    cursor: pointer;
                    font-size: 14px;
                    display: inline-block;
                    .td {
                        font-weight: bold;
                    }
                    &:last-child {
                        span {
                            &:last-child {
                                display: none;
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>